package mainServer;



import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
import org.omg.PortableServer.POA;

public class CorbaServer extends DefaultServer
{
  private int m_port;
  POA m_portableServer;
  ORB m_brocker;
  CorbaServerImpl m_impl;
  
  public CorbaServer(int port)
  {
    m_port = 1050; 
  }
  
  @Override
  public boolean start()
  {	  
	String[] args = new String[4];
	
	args[0] = "-ORBInitialPort";
	args[1] = "1050";
	args[2] = "-ORBInitialHost";
	args[3] = "127.0.0.1";
	  
	try 
	{
		m_brocker = ORB.init(args, null);
		m_portableServer = (POA)m_brocker.resolve_initial_references("RootPOA");
		m_portableServer.the_POAManager().activate();
		
		m_impl = new CorbaServerImpl(this);
		org.omg.CORBA.Object ref = m_portableServer.servant_to_reference(m_impl);
		org.omg.CORBA.Object namingContextObj = m_brocker.resolve_initial_references("NameService");
		
		NamingContext namingContext = NamingContextHelper.narrow(namingContextObj);			
		NameComponent[] path = { new NameComponent("JetGame", "Object") };		
		
		namingContext.rebind(path, ref);		
	}
	catch (Exception e) 
	{
		System.out.println("Невозможно создать CORBA");		
	    return false;
	}
	  
	return true;
  }

  @Override
  public boolean stop()
  {    
    return true;
  }
}
